﻿using App.Dal;
using App.Model;
using App.Untiys;
using SqlSugar;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Win.Dal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace App.Desk.Sys.User
{
    public partial class FrmEdit : UIEditForm
    {
        private bool CheckDropEmpty(UIIntegerUpDown uIIntegerUpDown, string retMsg)
        {
            if (uIIntegerUpDown.Value <= 0)
            {
                UIMessageBox.ShowWarning(retMsg);
                return false;
            }
            return true;
        }

        private SysUser userData;

        public SysUser UserData
        {
            get
            {
                if (userData == null)
                {
                    userData = new SysUser();
                }
                userData.Id = txtId.Text.Trim().ToInt();
                userData.ChName = txtChName.Text.Trim();
                userData.Acct = txtAcct.Text.Trim();
                userData.Disable = txtDisable.Active ? 1 : 0;
                userData.Abandon = txtAbandon.Active ? 1 : 0;
                userData.Phone = txtPhone.Text.Trim();
                return userData;
            }
            set
            {
                userData = value;
                txtId.Text = value.Id.ToStringEx();
                txtChName.Text = value.ChName;
                txtAcct.Text = value.Acct;
                txtDisable.Active = value.Disable == 1;
                txtAbandon.Active = value.Abandon == 1;
                txtPhone.Text = value.Phone;
            }
        }
        public FrmEdit(int id = 0)
        {
            InitializeComponent();
            BindDepart();
            BindRole();
            if (id <= 0)
                Text = "新增";
            else
            {
                Text = "修改";
                UserData = SysUserServices.Info(new SysUser { Id = id });
                for (int i = 0; i < txtDepart.Items.Count; i++)
                {
                    if (((ProDepart)txtDepart.Items[i]).Id == UserData.DepartId)
                    {
                        txtDepart.SelectedIndex = i;
                        break;
                    }
                }

                for (int i = 0; i < txtRole.Items.Count; i++)
                {
                    if (((SysRole)txtRole.Items[i]).Id == UserData.RoleId)
                    {
                        txtRole.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 查询可用 部门
        /// </summary>
        public void BindDepart()
        {
            var departs = ProDepartServices.GetList();
            txtDepart.Items.AddRange(departs.ToArray());
            txtDepart.DisplayMember = "DepartName";
            txtDepart.ValueMember = "Id";
        }

        /// <summary>
        /// 查询可用 角色
        /// </summary>
        public void BindRole()
        {
            var roles = SysRoleServices.GetList();
            txtRole.Items.AddRange(roles.ToArray());
            txtRole.DisplayMember = "RoleName";
            txtRole.ValueMember = "Id";
        }

        private void FrmEdit_ButtonOkClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAcct.Text.Trim()))
            {
                txtAcct.Focus();
                UIMessageTip.ShowWarning("请输入账号！"); return;
            }
            if (SysUserServices.Isrepeat(UserData.Id, txtAcct.Text.Trim()))
            {
                txtAcct.Focus();
                UIMessageTip.ShowWarning("账号重复，请重新输入！"); return;
            }
            if (string.IsNullOrEmpty(txtChName.Text.Trim()))
            {
                txtChName.Focus();
                UIMessageTip.ShowWarning("请输入姓名！"); return;
            }
            if (txtDepart.SelectedIndex < 0)
            {
                txtDepart.Focus();
                UIMessageTip.ShowWarning("请选择部门！"); return;
            }
            if (UserData.Id <= 0)
            {
                if (!CheckPassword())
                    return;
            }
            else
            {
                if (!string.IsNullOrEmpty(txtPwd.Text.Trim()))
                {
                    if (!CheckPassword())
                        return;
                }
            }
            
            UserData.AddUser = Config.CurrentUser.Id;
            UserData.DepartId = ((ProDepart)txtDepart.SelectedItem).Id;
            UserData.RoleId = ((SysRole)txtRole.SelectedItem).Id;
            if (SysUserServices.Edit(UserData))
            {
                UIMessageBox.ShowSuccess("保存成功！");
                this.Close();
            }
            else
                UIMessageBox.ShowError("保存失败！");
        }

       

        /// <summary>
        /// 检验密码
        /// </summary>
        /// <returns></returns>
        public bool CheckPassword()
        {
            UserData.Pwd = "";
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                txtPwd.Focus();
                UIMessageTip.ShowWarning("请输入密码！");
                return false;
            }
            //if (!DataTranf.IsPasswordComplex(txtPwd.Text.Trim()))
            //{
            //    txtPwd.Focus();
            //    UIMessageTip.ShowWarning("密码必须8位以上且包含字母大写、字母小写、数字、特殊字符！");
            //    return false;
            //}
            if (string.IsNullOrEmpty(txtCPwd.Text.Trim()))
            {
                txtCPwd.Focus();
                UIMessageTip.ShowWarning("请再次输入密码！");
                return false;
            }
            if (txtPwd.Text.Trim() != txtCPwd.Text.Trim())
            {
                txtCPwd.Focus();
                UIMessageTip.ShowWarning("两次输入密码不一致！");
                return false;
            }
            UserData.Pwd = DesEncrypt.Encrypt(txtPwd.Text.Trim());
            return true;
        }
    }
}
